package com.jader.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jader.beans.OrderDetail;
import com.jader.util.JaderApplication;
import com.zlp.jader.R;

public class OrderAdapter extends BaseExpandableListAdapter {
	Context context;
	List<String> parent_sellerName = new ArrayList<String>();
	Map<String, List<OrderDetail>> childOrder = new HashMap<String, List<OrderDetail>>();
	LayoutInflater inflater;

	public OrderAdapter(List<String> list, Map<String, List<OrderDetail>> map,
			Context context) {
		super();
		this.parent_sellerName = list;
		this.childOrder = map;
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return parent_sellerName.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		String parent = parent_sellerName.get(groupPosition);
		int childCount = childOrder.get(parent).size();
		Log.e("OrderChildCount", "" + childCount);
		return childCount;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return parent_sellerName.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		String parent = parent_sellerName.get(groupPosition);
		return (childOrder.get(parent).get(childPosition));
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater
					.inflate(R.layout.listview_order_parent, null);
		}
		TextView sellerName = (TextView) convertView
				.findViewById(R.id.tv_orderparent_sellername);
		String name = this.parent_sellerName.get(groupPosition);
		Log.e("orderSellerName", name);
		sellerName.setText(name);
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		String sellername = parent_sellerName.get(groupPosition);
		OrderDetail orderInfo = childOrder.get(sellername).get(childPosition);
		convertView = inflater.inflate(R.layout.listview_order_child, null);
		ImageView order_picture = (ImageView) convertView
				.findViewById(R.id.img_listview_order_goodpic);
		TextView order_goodname = (TextView) convertView
				.findViewById(R.id.tv_listview_order_goodname);
		TextView order_goodprcie = (TextView) convertView
				.findViewById(R.id.tv_listview_order_goodprice);
		TextView order_state = (TextView) convertView
				.findViewById(R.id.tv_listview_order_goodstate);
		TextView order_gooddesc = (TextView) convertView
				.findViewById(R.id.tv_listview_order_gooddesc);
		TextView order_goodnum = (TextView) convertView
				.findViewById(R.id.tv_listview_order_goodnum);
		TextView order_date = (TextView) convertView
				.findViewById(R.id.tv_listview_orderdate);
		JaderApplication.getBitmapUtils().display(order_picture,
				JaderApplication.getIP()+"/Jader" + orderInfo.getGoods().getPicture());
		String goodname = orderInfo.getGoods().getGoodsName();
		double goodprice = orderInfo.getGoods().getPrice();
		String goodstate = orderInfo.getStatus();
		int goodnum = orderInfo.getNumber();
		String gooddesc = orderInfo.getGoods().getDesc().trim();
		String date = orderInfo.getTime();
		order_gooddesc.setText(gooddesc);
		order_goodnum.setText("x" + goodnum);
		order_goodname.setText(goodname);
		order_goodprcie.setText("￥ " + goodprice);
		String state = null;
		if ("1".equals(goodstate)) {
			state = "未付款";
		} else {
			state = "已付款";
		}
		order_state.setText(state);
		order_date.setText(date);
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}
}
